'''
Update the daily historic data. Add a new row for the unrecorded days util today.
The new row include Date, Open, High, Low, Close, Volume, Adj Close

Created on April, 2012
@author: bruce
'''

import os
import glob
from datetime import date, timedelta

from src.util.logutil import LogFile
from src.util.datacollectutil import YahooFinance

class DailyUpdate:
    def __init__(self, output_dir="output\\"):          
        self.__output_dir = output_dir                
        self.__daily_log = LogFile("daily update - date and symbols log")
        self.__today = date.today()
        self.__stock_quotes = YahooFinance()   
        self.__symbols = []        
        self.__get_symbols()
        self.__daily_log.plain_log(str(self.__today.month) + " " + str(self.__today.day) + 
                                   " " + str(self.__today.year))    
    def __close_logs(self):
        self.__daily_log.closelog()
        self.__stock_quotes.uninitialize()
    
    """
    Get symbols from the csv file directory.
    """    
    def __get_symbols(self):             
        for file_path in glob.glob(os.path.join(self.__output_dir, '*.csv')):   
            filename = os.path.basename(file_path)
            self.__symbols.append(os.path.splitext(filename)[0])

    """
    Append the historical quotes from the last update.
    """
    def __write_histquotes(self, start_date, end_date, symbol, file_path):
        quote_lines = []
        symbol_quotes = self.__stock_quotes.gethistquotes(symbol, 
                                                          (start_date.month, start_date.day, start_date.year),
                                                          (end_date.month, end_date.day, end_date.year))
        if len(symbol_quotes) != 0:  # empty quote string means we failed in the http request.
            output_file = open(file_path, 'a')
            quote_lines = symbol_quotes.split('\n')
            del quote_lines[0]
            quote_lines.reverse()
            for line in quote_lines:
                if len(line.strip()) != 0:
                    output_file.write(line + "\n")  
            self.__daily_log.plain_log(symbol) 
            output_file.close()   
                    
    def run(self):
        one_day = timedelta(days = 1)
        end_date = self.__today
        while 1:
            wkday = end_date.isoweekday() # ranges from 1 = monday to 7 = sunday
            if wkday >= 1 and wkday <= 5:
                break
            end_date -= one_day
            
        for symbol in self.__symbols:
            # Check for the last update date and get the request start date.
            try:            
                file_path = self.__output_dir + symbol + ".csv"
                inp = open(file_path, 'r')
                lines = inp.readlines()
                inp.close()
                last_date_raw = lines[-1].split(',')[0]
                (y, m, d) = last_date_raw.split('-')
                last_date = date(int(y), int(m), int(d))
                start_date = last_date + one_day
                
                if end_date >= start_date:        
                    self.__write_histquotes(start_date, end_date, symbol, file_path)
            except:
                self.__daily_log.detail_log('Error', 'Failed in getting update for symbol: ' + symbol)  
        self.__close_logs()
                    
if __name__ == "__main__":  
    daily_up = DailyUpdate()
    daily_up.run()